﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Data;

namespace Web.Paper
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)Session["Account"]).RoleCurrent.Equals('s')) Response.Redirect("Default.aspx");
        
            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(((Student)Session["Student"]).StudentID, ((Student)Session["Student"]).GraduateYear);
            if (myPaper.TeacherID.Trim() == "")
            {
                ImageButton_upload.Visible = false;
                Label_result.Text = "你还没有指导老师";
            }

            int str=int.Parse(Request.QueryString["style"]);
            Label_PDeadLineResult.Text= DeadLine.GetDeadLineDate(str).ToString();
            Label_PDeadLineResultTrue.Text = DeadLine.GetDeadLineDate(str).ToString();
            
            PaperFile myPaperFile=new PaperFile(str,(((Student)Session["Student"]).StudentID),myPaper.TeacherID);
            DataSet ds = myPaperFile.GetConversationFile();
            
            if (ds.Tables[0].Rows.Count == 0)
                Label_NoData.Text = "你未提交该类型文档";
            else
            {
                Label_NoData.Text = "";
                GridView_DocFile.DataSource = ds;
                GridView_DocFile.DataBind();
            }

            switch (str)
            {
                case 1: Label_title.Text = Resources.Docs.Doc1; break;
                case 2: Label_title.Text = Resources.Docs.Doc2; break;
                case 3: Label_title.Text = Resources.Docs.Doc3; break;
                case 4: Label_title.Text = Resources.Docs.Doc4; break;
                case 5: Label_title.Text = Resources.Docs.Doc5; break;
                case 6: Label_title.Text = Resources.Docs.Doc6; break;
                default: break;
            }
            
            DateTime DeadLineTime=DateTime.Parse(DeadLine.GetDeadLineDate(str).ToString());
            if (DateTime.Compare(DateTime.Now, DeadLineTime) > 0)
            {
                ImageButton_upload.Enabled = false;
                Label_PDeadLineResult.Visible = true;
                Label_PDeadLineResultTrue.Visible = false;
            }
        }

        protected void ImageButton_download_Click(object sender, ImageClickEventArgs e)
        {
           
            int str = int.Parse(Request.QueryString["style"]);
            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(((Student)Session["Student"]).StudentID, ((Student)Session["Student"]).GraduateYear);
            PaperFile myPaperFile = new PaperFile(str, (((Student)Session["Student"]).StudentID), myPaper.TeacherID);
            myPaperFile.GetOneFile(decimal.Parse(((ImageButton)sender).CommandArgument.ToString()));
        }

        protected void ImageButton_upload_Click(object sender, ImageClickEventArgs e)
        {
            
            if (FileUpload1.PostedFile.ContentLength == 0)
            {
                Label_NoUploadFile.Text = "请选择上传文件";
                Label1.Visible = false;
            }
            else
            {
                System.IO.Stream myStream;
                myStream = FileUpload1.FileContent;

                string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1);
                string type = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("."));

                string gettype = System.IO.Path.GetExtension(FileUpload1.FileName);
                if ((gettype == ".rar") || (gettype == ".zip") || (gettype == ".doc"))
                {
                    int str = int.Parse(Request.QueryString["style"]);
                    GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(((Student)Session["Student"]).StudentID, ((Student)Session["Student"]).GraduateYear);
                    PaperFile myPaperFile = new PaperFile(str, (((Student)Session["Student"]).StudentID), myPaper.TeacherID);
                    string ok = myPaperFile.UploadFile(TextBox_comment.Text.ToString(), ((Account)Session["Account"]).RoleCurrent.ToString(), myStream, type);
                    if (ok.CompareTo(Resources.ErrorMessage.OK) == 0)
                    {
                        string ok1 = myPaperFile.ChangeStatus(2, null);
                        if (ok1.CompareTo(Resources.ErrorMessage.OK) == 0)
                            Label_result.Text = "提交成功";
                        else Label_result.Text = "提交未成功";
                    }
                    else Label_result.Text = "提交未成功";
                    TextBox_comment.Text = "";
                    Label_NoUploadFile.Text = "";
                    Label1.Visible = false;
                }
                else
                    Label1.Visible = true;
            }
        }

        protected void ImageButton_readcomment_Click(object sender, ImageClickEventArgs e)
        {
            

            int str = int.Parse(Request.QueryString["style"]);
            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(((Student)Session["Student"]).StudentID, ((Student)Session["Student"]).GraduateYear);
            
            
        }

        protected void GridView_DocFile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            
            GridView_DocFile.PageIndex = e.NewPageIndex;
            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(((Student)Session["Student"]).StudentID, ((Student)Session["Student"]).GraduateYear);
            int str = int.Parse(Request.QueryString["style"]);
            PaperFile myPaperFile = new PaperFile(str, (((Student)Session["Student"]).StudentID), myPaper.TeacherID);
            DataSet ds = myPaperFile.GetConversationFile();
            GridView_DocFile.DataSource = ds;
            GridView_DocFile.DataBind();

        }
    }
}
